package com.origin.chaos.security.controller;

import com.origin.chaos.common.bean.vo.Result;
import com.origin.chaos.security.bean.vo.LoginVo;
import com.origin.chaos.security.bean.vo.RegisterVo;
import com.origin.chaos.security.bean.vo.ResetPsdVo;
import com.origin.chaos.security.bean.vo.UserInfoVo;
import com.origin.chaos.security.serivce.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: tao.ding
 * @date 2021-07-16
 */
@RestController
@RequestMapping("/api/auth")
@Api(tags = "认证鉴权模块")
@Slf4j
@Validated
public class AuthController {

    private IUserService userService;

    @GetMapping("/register-code/{email}")
    @ApiOperation(value = "发送注册验证码", notes = "返回验证码失效时间，单位秒")
    public Result<Long> registerCode(@PathVariable("email") String email) {
        return this.userService.sendRegisterCode(email);
    }

    @GetMapping("/reset-psd-code/{email}")
    @ApiOperation(value = "发送重置密码验证码", notes = "返回验证码失效时间，单位秒")
    public Result<Long> resetPsdCode(@PathVariable("email") String email) {
        return this.userService.sendResetPsdCode(email);
    }

    @PostMapping("/register")
    @ApiOperation("注册用户")
    public Result<UserInfoVo> register(@Validated @RequestBody RegisterVo vo) {
        return Result.ofBody(this.userService.register(vo));
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "返回token")
    public Result<String> login(@Validated @RequestBody LoginVo vo) {
        return Result.ofBody(this.userService.login(vo));
    }

    @PostMapping("/reset-psd")
    @ApiOperation("重置密码")
    public Result<UserInfoVo> resetPsd(@Validated @RequestBody ResetPsdVo vo) {
        return Result.ofBody(this.userService.resetPsd(vo));
    }


    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
}
